﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using MaxLeaf.Platform.DALImplementation;
using MaxLeaf.Platform.Functions;
using CommonsLib.Utility;
using MaxLeaf.Platform.Commons;
using System.Data.Common;
using System.Data.SqlClient;
using System.Transactions;
using System.Data;

namespace MaxLeaf.Platform.BLLImplementation
{
    public class goods_SCPCategoryBLL : BaseBLL
    {
         
        public override object Add(object model)
        {

            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {
                
                object obj = base.Add(model);

                using (DBAccess dbcon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase)))
                {
                    goods_SCPCategoryDataModel data = (goods_SCPCategoryDataModel)model;
                    AddChild(dbcon, data);


                }

                ts.Complete();

                return obj;
            }
        }

        public override object Update(object model)
        {

            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {
                using (DBAccess dbcon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase)))
                {
                    goods_SCPCategoryDataModel data = (goods_SCPCategoryDataModel)model;
                    goods_SCPCategoryDataModel olddata =(goods_SCPCategoryDataModel)Select(new goods_SCPCategoryDataModel(),data.Key);
                    if (olddata.SCPCategoryId != data.SCPCategoryId || olddata.GoodsId != data.GoodsId)
                    {
                        DeleteChild(dbcon, olddata);

                        AddChild(dbcon, data);
                    }

                }
                object obj = base.Update(model);
                ts.Complete();

                return obj;
            }
        }

        private void AddChild(DBAccess dbcon, goods_SCPCategoryDataModel parentData)
        {
            DataTable dt = GetChildSCP(dbcon, parentData.SCPCategoryId);
            foreach (DataRow dr in dt.Rows)
            {
                goods_SCPCategoryDataModel child = new goods_SCPCategoryDataModel() { GoodsId = parentData.GoodsId, SCPCategoryId = CvtFunc.ToInt(dr["FinterID"]) };

                base.Add(child);

            }
        }

        private void DeleteChild( DBAccess dbcon,goods_SCPCategoryDataModel parentData)
        {
            DataTable dt = GetChildSCP(dbcon, parentData.SCPCategoryId);
            foreach (DataRow dr in dt.Rows)
            {
                foreach (goods_SCPCategoryDataModel child in SelectByCond(new goods_SCPCategoryDataModel()
                    , new List<AddInfo>() { 
                                new AddInfo{ Name= AddInfoType.Cond ,Value= new List<Cond>() { 
                                        new Cond { PropName ="GoodsId", Compare = CompareType.Equal, Value = parentData.GoodsId }
                                        ,new Cond { PropName ="SCPCategoryId", Compare = CompareType.Equal, Value =CvtFunc.ToInt(dr["FinterID"]) }
                                    }
                                } 
                            }
                ).Data)
                {
                    base.Delete(child);
                }
            }
        }

        public override bool Delete(object model)
        {
            using (System.Transactions.TransactionScope ts = DataFunc.CreateTransactionScope())
            {
                using (DBAccess dbcon = new DBAccess(DataFunc.GetConnectionInfo(Const.DB_SCPBase)))
                {
                    DeleteChild(dbcon, (goods_SCPCategoryDataModel)model);
                }
                bool blnRT= base.Delete(model);
                ts.Complete();

                return blnRT;
            }
        }

        private static DataTable GetChildSCP(DBAccess dbcon, int intFinterID)
        {
            string strSql = @"select distinct a.FinterID from SCPCategory a  inner join  SCPCategory b  on  dbo.F_GetParentCompanyID(a.CompanyID)=b.CompanyID and b.CompanyID<>0  and a.SCPCategorySeqNo=b.SCPCategorySeqNo
                        where b.FinterID=@FinterID";
            DataTable dt = dbcon.GetDataTable(strSql, new SqlParameter[]{
                        new SqlParameter("@FinterID",intFinterID.ToString())
                    });
            return dt;
        }

    }
}
